<?php

namespace addons\exam\model;


use addons\exam\enum\ScoreGoodStatus;

class ScoreGoodModel extends \app\admin\model\exam\ScoreGoodModel
{
    /**
     * 减库存
     * @param $good
     * @param $quantity
     * @return bool
     */
    public static function decrement($good, $quantity)
    {
        if ($good['stocks'] < 1) {
            return false;
        }
        if ($good['stocks'] - $quantity < 0) {
            return false;
        }

        return self::transaction(function () use ($good, $quantity) {
            $good->stocks -= $quantity;
            // 售罄
            if ($good->stocks == 0) {
                $good->status = ScoreGoodStatus::SELL_OUT;
            }
            return $good->save();
        });
    }
}
